package com.bong.ring.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by panpan on 2018/2/7.
 */

public class ProgressView extends View {
    private float percent = 1.5f;

    public ProgressView(Context context) {
        super(context);
        init();
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(21)
    public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    Paint paint;
    boolean beginAnim = false;

    private void init() {
        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(8);
    }


    float tempSweep = 0;
    int tempAngel = 0;
    boolean increse = true;

    @TargetApi(21)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (beginAnim) {
            tempAngel += 5;
            tempAngel %= 360;
            canvas.rotate(tempAngel, canvas.getWidth() / 2, canvas.getHeight() / 2);
            if (increse) {
                tempSweep += 5;
                if (tempSweep > 355) {
                    increse = false;
                    tempSweep = 355;
                }
                canvas.drawArc(paint.getStrokeWidth() / 2, paint.getStrokeWidth() / 2, getWidth() - paint.getStrokeWidth() / 2, getHeight() - paint.getStrokeWidth() / 2, -90, tempSweep, false, paint);
            } else {
                tempSweep -= 5;
                if (tempSweep < 5) {
                    increse = true;
                    tempSweep = 5;
                }
                canvas.drawArc(paint.getStrokeWidth() / 2, paint.getStrokeWidth() / 2, getWidth() - paint.getStrokeWidth() / 2, getHeight() - paint.getStrokeWidth() / 2, -90 + 358, -tempSweep, false, paint);
            }
            invalidate();
        } else {
            if (percent >= 1) {
                canAnim = true;
                canvas.drawCircle(getWidth() / 2, getHeight() / 2, (getWidth() - paint.getStrokeWidth()) / 2, paint);
            } else
                canvas.drawArc(paint.getStrokeWidth() / 2, paint.getStrokeWidth() / 2, getWidth() - paint.getStrokeWidth() / 2, getHeight() - paint.getStrokeWidth() / 2, -90, 360 * percent, false, paint);
        }
    }

    boolean canAnim = false;

    public void beginAnim(boolean begin) {
        if (canAnim) {
            tempAngel = 0;
            increse = true;
            tempSweep = 0;
            beginAnim = begin;
            invalidate();
        }
    }

    public void setPercent(float percent) {
        if (canAnim) {

        } else {
            beginAnim = false;
            this.percent = percent;
            invalidate();
        }
    }
    public void reset()
    {
        tempAngel = 0;
        increse = true;
        tempSweep = 0;
        beginAnim = false;
        canAnim = false;
        percent=0;
        invalidate();
    }
}
